package org.quadbac.converter;

import org.quadbac.measurements.*;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class AddUnitOffset extends Activity {
	private TextView newUnitText;
	private EditText editName;
	private EditText editSymbol;
	private EditText editMultiplier;
	private Button buttonNewUnit;
	private EditText editOffset;
	private Button buttonRefUnit;
	private MeasurementType mType;
	private Unit unit2;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addunitoffset);
        Bundle b = getIntent().getExtras();
        mType = (MeasurementType) b.getSerializable("mType");
        unit2 = mType.getTypeUnits().get(0);
        
        newUnitText = (TextView) findViewById(R.id.newUnitText);
        editName = (EditText) findViewById(R.id.editName);
        editSymbol = (EditText) findViewById(R.id.editSymbol);
        editMultiplier = (EditText) findViewById(R.id.editMultiplier);
        buttonNewUnit = (Button) findViewById(R.id.buttonNewUnit);
        editOffset = (EditText) findViewById(R.id.editOffset);
        buttonRefUnit = (Button) findViewById(R.id.buttonRefUnit);
        registerForContextMenu(buttonRefUnit);
        
        newUnitText.setText("New "+mType.getTypeName()+" Unit");
        editName.setText("");
        editSymbol.setText("");
        editMultiplier.setText("");
        buttonNewUnit.setText("");
        editOffset.setText("");
        buttonRefUnit.setText(unit2.getUnitFullName());
        editName.addTextChangedListener(nameChangeListener);
        editSymbol.addTextChangedListener(nameChangeListener);
    }
	
	TextWatcher nameChangeListener = new TextWatcher(){

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			String unitLabel = editName.getText().toString();
			if (editSymbol.getText().length() != 0){
				unitLabel = unitLabel+" ("+editSymbol.getText()+")";
			}
			buttonNewUnit.setText(unitLabel);
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {}

		@Override
		public void afterTextChanged(Editable s) {}
		
    };
    
    public void unitButtonHandler(View view) {
    	openContextMenu(view);
    }
 
    public void createButtonHandler(View view) {
    	if (validateNewUnit()){
    		ReversibleTransform<Double> baseTransform = unit2.getUnitTransform();
        	Double multiplier = Double.valueOf(editMultiplier.getText().toString());
        	Double offset = Double.valueOf(editOffset.getText().toString());
        	LinearTransform newTransform = new LinearTransform((LinearTransform)baseTransform, offset, multiplier);
        	Unit newUnit = new Unit(editName.getText().toString(), editSymbol.getText().toString(),newTransform);
        	getIntent().putExtra("newUnit", newUnit);
        	setResult(RESULT_OK, getIntent());
        	finish();
    	}
    }
    
    private boolean validateNewUnit() {
    	
    	if (editName.getText().length() == 0){
			editName.requestFocus();
			Toast t = Toast.makeText(this,"Please enter a name",Toast.LENGTH_LONG);
	    	t.setGravity(Gravity.CENTER, 0, 0);
			t.show();
			return false;
		}
		if (editMultiplier.getText().length() == 0){
			editMultiplier.requestFocus();
			Toast t = Toast.makeText(this,"Please enter a multiplier",Toast.LENGTH_LONG);
	    	t.setGravity(Gravity.CENTER, 0, 0);
			t.show();
			return false;
		}
		if (Double.valueOf(editMultiplier.getText().toString()) == 0){
			editMultiplier.requestFocus();
			Toast t = Toast.makeText(this,"Multiplier must be non-zero",Toast.LENGTH_LONG);
	    	t.setGravity(Gravity.CENTER, 0, 0);
			t.show();
			return false;
		}
		if (editOffset.getText().length() == 0){
			editOffset.requestFocus();
			Toast t = Toast.makeText(this,"Please enter an offset",Toast.LENGTH_LONG);
	    	t.setGravity(Gravity.CENTER, 0, 0);
			t.show();
			return false;
		}
		return true;
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
// Create unit menu
		menu.setHeaderTitle("Select Unit");
		int size = mType.getTypeUnits().size();
		for(int x=0; x < size; x++){
			menu.add(0,x,0,mType.getTypeUnits().get(x).getUnitFullName());
		}
	}
    
	@Override
	public boolean onContextItemSelected(MenuItem item) {
// 		item.ItemId was set in onCreateContextMenu to the index of the
//		selected unit
		Unit selUnit = mType.getTypeUnits().get(item.getItemId());
		buttonRefUnit.setText(selUnit.getUnitFullName());
		unit2 = selUnit;
		return super.onContextItemSelected(item);
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.addunitmenu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()){
		case R.id.switchmethod :
			getIntent().putExtra("switchmethod", true);
			setResult(RESULT_OK, getIntent());
			finish();
			break;
		}
		return super.onOptionsItemSelected(item);
	}

    
}